<!--站点列表页-->
<template>
    <div class="wrap">
        <m-page-list
                :page="page"
                :total="page.total"
                :option="option"
                :table-data="tableData"
                :table-loading="tableLoading"
        />
        <m-dialog-form ref="dialogRef" :data="dialogData" v-model:show="dialogShow" :option="dialogOption" @confirm="dialogConfirm" ></m-dialog-form>
    </div>

</template>

<script setup>

  import {defineProps,watch} from 'vue'
  //组件
  import MPageList from '@/components/PageList'
  import MDialogForm from '@/components/DialogForm/DialogForm'
  //请求
  import {getShuttlePackage, addShuttlePackage,editShuttlePackage,delShuttlePackage,delShuttlePackageBySid,getTicketPackageType,getTicketType,publicAllData} from '@/request/api/basicData'

  import {usePageList} from "./relPage";
  //引用类型字段参数
  import {refOpt} from "../const";

  const props = defineProps(['shuttleData']);

  let shuttleSeatId = '';
  let opts = {
    auth_id: 'shuttlePackage',
    getApi: getShuttlePackage,addApi:addShuttlePackage,editApi:editShuttlePackage,delApi:delShuttlePackage,
    idKey: ['shuttle_id','packag_type_id','ticket_typall_id'],
    defQuery: '',
    cols: [
      {prop: 'shuttle_name',label: '班车名称' },
      {prop: 'packag_type_name',label: '套票类型名称' },
      {prop: 'ticket_typall_name',label: '票种名称' },
      {prop: 'Order_amount',label: '订单金额',type: 'number',precision: 2 },
      {prop: 'effective_time_start',label: '车票有效时间起' },
      {prop: 'effective_time_end',label: '车票有效时间止' },
    ],
    confirmCallback:() => publicAllData(shuttleSeatId),
    deleteCallback: () => publicAllData(shuttleSeatId)
  };
  let {option,tableLoading,tableData,query,page,dialogShow,dialogData,dialogOption,defDialogData,dialogRef,isDelete,
    dialogConfirm,
    getDataList,} = usePageList(opts);


  watch(() => props.shuttleData, (newVal) => {
    if (newVal){
      let {shuttle_id,shuttle_name,shuttle_seat_id} = newVal;
      query.shuttle_id = shuttle_id;
      defDialogData.shuttle_id = shuttle_id;
      defDialogData.shuttle_name = shuttle_name;
      getDataList();
      shuttleSeatId = shuttle_seat_id || shuttle_id
    }
  },{immediate: true});

  // option.hRBtns.push({title: '清空',id: 'build',type: 'primary',event: clearAll});
  dialogOption.fieldList =  [
    {field_id: 'shuttle_id',label: '班车编号',type: 'text',col: 3 ,required: true,disabled: true },
    {field_id: 'shuttle_name',label: '班车名称',type: 'text',col: 3 ,required: true,disabled: true },
    {...refOpt.packag_type_id,required: true,col:3},
    {...refOpt.packag_type_name,required: true,col:3,valueKey: ['packag_type_id',{packag_type_name: `${props.shuttleData.shuttle_type === '定制'? 'custom_packag_type_name' : 'packag_type_name'}`}]},
    {...refOpt.ticket_typall_id,required: true,col:3},
    {...refOpt.ticket_typall_name,required: true,editDisabled: true,col:3},
    {field_id: 'Order_amount',label: '订单金额',type: 'number',col: 3 ,required: true,precision: 2 },
    {field_id: 'effective_time_start',label: '车票有效时间起',type: 'datetime',col: 3 ,required: true },
    {field_id: 'effective_time_end',label: '车票有效时间止',type: 'datetime',col: 3 ,required: true }
    ]
    prepare();
  //删除所有套票
  function clearAll(){
    if (!isDelete.value){
      ElMessage({
        type: 'warning',
        message: '抱歉，您没有权限删除该模块数据'
      })
      return
    }
    ElMessageBox.confirm('确定删除该班次所有套票？', '清空', {
      'cancelButtonText': '取消',
      'confirmButtonText': '确定',
      callback: (action) => {
        if (action === 'confirm'){
          delShuttlePackageBySid(query.shuttle_id).then(res => {
            if (res.errcode !== 0)return;
            ElMessage({
              type: 'success',
              message: `删除成功`,
            });
            getDataList();
            publicAllData(shuttleSeatId);
          })
        }
      },
    })
  }

  function prepare() {
    isDelete.value && option.hRBtns.push({title: '清空',id: 'build',type: 'primary',event: clearAll});
  }

</script>
<style scoped lang="less">
    .wrap{
        height: 100%;
        ::v-deep{
            .layout-header{
                padding: 0;
            }
            .layout-main{
                padding: 4px 0 0;
            }
        }
    }

</style>
